﻿using System;
using System.IO;
using UnityEngine;

namespace UFrameWork
{
    public class UnityFile
    {
        private static string _android = string.Empty;

        private static string _path = string.Empty;

        /// <summary>
        /// 获取文件写入路径
        /// </summary>
        public static string Path 
        {
            get
            {
                if (string.IsNullOrEmpty(_path)) _SetPath();
                return _path;
            }
        }

        private static void _SetPath()
        {
            if (string.IsNullOrEmpty(_android))
            {
                _android = Application.productName;
            }

            switch (Application.platform)
            {
                case RuntimePlatform.WindowsEditor:
                case RuntimePlatform.OSXEditor:
                case RuntimePlatform.WindowsPlayer:
                    _path = Application.dataPath + "/../";
                    break;
                case RuntimePlatform.IPhonePlayer:
                    _path = Application.persistentDataPath + "/";
                    break;
                case RuntimePlatform.Android:
                    _path = "/sdcard/" + _android + "/";
                    break;
            }
        }

        /// <summary>
        /// 设置android的文件夹名字
        /// </summary>
        /// <param name="dir">文件夹名字</param>
        public static void SetAndroidDir(string dir)
        {
            _android = dir;
            _SetPath();
        }

        /// <summary>
        /// 获取文件路径
        /// </summary>
        public static string GetPath(string file)
        {
            file = Path + file;
            new FileInfo(file).Directory.Create();
            return file;
        }
    }
}